package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Result;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService ;

    @PostMapping("/shoppingCart/add")
    @ResponseBody
    private Result addCart(@RequestBody ShoppingCart shoppingCart , HttpSession httpSession ){

        Long user = (Long) httpSession.getAttribute("user");

        shoppingCart.setUserId(user);

        ShoppingCart shoppingCart1 = shoppingCartService.addCart(shoppingCart);

        return Result.success(shoppingCart1);

    }

    @GetMapping("/shoppingCart/list")
    @ResponseBody
    private Result list( HttpSession httpSession ){

        //获取用户id
        Long userId = (Long) httpSession.getAttribute("user");
        //然后获取购物车数据列表
        List<ShoppingCart> shoppingCartList = shoppingCartService.list( userId );

        return Result.success(shoppingCartList);
    }

}
